using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Legends;
using OxyPlot.Series;
using OxyPlot.Wpf;
using ScottPlot;
using ScottPlot.AxisPanels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using WHC.Framework.Commons;

namespace SugarProject.Core
{
    /// <summary>
    /// 生成图形的帮助类
    /// </summary>
    public class ChartHelper
    {
        /// <summary>
        /// 一个x 三个y轴数据
        /// </summary>
        /// <param name="xValues"></param>
        /// <param name="y1Values"></param>
        /// <param name="y2Values1"></param>
        /// <param name="y2Values2"></param>
        /// <param name="xAxisTitle"></param>
        /// <param name="y1AxisTitle"></param>
        /// <param name="y2AxisTitle"></param>
        /// <param name="legend1"></param>
        /// <param name="legend2"></param>
        /// <param name="legend3"></param>
        /// <param name="outputPath"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static void GenerateDualAxisTripleLineChart(
            List<double> xValues,
            List<double> y1Values,
            List<double> y2Values1,
            List<double> y2Values2,
            string xAxisTitle,
            string y1AxisTitle,
            string y2AxisTitle,
            string legend1,
            string legend2,
            string legend3,
            string outputPath,
            int width = 800,
            int height = 500
        )
        {
            if (
                xValues.Count != y1Values.Count
                || xValues.Count != y2Values1.Count
                || xValues.Count != y2Values2.Count
            )
            {
                throw new ArgumentException("X轴和所有Y轴的数据点数量必须相同");
            }

            // 创建一个新的绘图对象
            var plt = new Plot();

            var sig1 = plt.Add.SignalXY(xValues.ToArray(), y1Values.ToArray());
            var sig2 = plt.Add.SignalXY(xValues.ToArray(), y2Values1.ToArray());
            var sig3 = plt.Add.SignalXY(xValues.ToArray(), y2Values2.ToArray());

            double y2MaxValue = Math.Max(y2Values1.Max(), y2Values2.Max());

            sig1.Color = Color.FromARGB(0xFF3B88BD); // 蓝色
            sig2.Color = Color.FromARGB(0xFFFF7F0E); // 橙色
            sig3.Color = Color.FromARGB(0xFFA9A9A9); // 灰色

            // 设置线条宽度
            sig1.LineWidth = 2;
            sig2.LineWidth = 2;
            sig3.LineWidth = 2;

            // 设置轴关联
            // 创建右侧轴
            RightAxis rightAxis = new RightAxis();
            sig1.Axes.YAxis = plt.Axes.Left;
            sig2.Axes.YAxis = plt.Axes.Right;
            sig3.Axes.YAxis = plt.Axes.Right;

            sig1.LegendText = legend1;
            sig2.LegendText = legend2;
            sig3.LegendText = legend3;

            // 设置图表标题和轴标题            
            plt.XLabel(xAxisTitle, size: 18);
            plt.Axes.Left.Label.Text = y1AxisTitle;
            plt.Axes.Right.Label.Text = y2AxisTitle;

            // 设置X轴、Y轴刻度字体大小
            plt.Axes.Bottom.TickLabelStyle.FontSize = 16;
            plt.Axes.Left.TickLabelStyle.FontSize = 16;
            plt.Axes.Right.TickLabelStyle.FontSize = 16;

            // 设置X轴的最小值和最大值
            // 计算左侧Y轴（电压）的范围和刻度
            int tickCount = 8;
            double y1Min = y1Values.Min();
            double y1Max = y1Values.Max() * 1.1;
            double y1Range = y1Max - y1Min;
            int y1TickCount = tickCount;
            double y1TickSpacing = y1Range / y1TickCount;

            // 计算右侧Y轴（电流）的范围和刻度
            double y2Min = y2Values2.Min() > y2Values1.Min()? y2Values1.Min(): y2Values2.Min();
            double y2Max = Math.Max(y2Values1.Max(), y2Values2.Max()) * 1.1;
            double y2Range = y2Max - y2Min;
            int y2TickCount = tickCount;
            double y2TickSpacing = y2Range / y2TickCount;

            // 设置左侧Y轴的刻度
            string y1TickFormat = CalcTickFormat(y1TickSpacing);
            plt.Axes.SetLimitsY(y1Min, y1Max);
            plt.Axes.Left.TickGenerator = new ScottPlot.TickGenerators.NumericManual(
                Enumerable
                    .Range(0, y1TickCount + 1)
                    .Select(i => y1Min + i * y1TickSpacing)
                    .ToArray(),
                Enumerable
                    .Range(0, y1TickCount + 1)
                    .Select(i => (y1Min + i * y1TickSpacing).ToString(y1TickFormat))
                    .ToArray()
            );

            // 设置右侧Y轴的刻度
            string y2TickFormat = CalcTickFormat(y2TickSpacing);
            plt.Axes.Right.Max = y2Max;
            plt.Axes.Right.Min = 0;
            plt.Axes.Right.TickGenerator = new ScottPlot.TickGenerators.NumericManual(
                Enumerable
                    .Range(0, y2TickCount + 1)
                    .Select(i => y2Min + i * y2TickSpacing)
                    .ToArray(),
                Enumerable
                    .Range(0, y2TickCount + 1)
                    .Select(i => (y2Min + i * y2TickSpacing).ToString(y2TickFormat))
                    .ToArray()
            );

            plt.Axes.SetLimitsX(xValues.Min(), xValues.Max());

            // 设置字体，确保使用支持中文的字体
            string fontName = "宋体";
            plt.Axes.Title.Label.FontName = fontName;
            plt.Axes.Bottom.Label.FontName = fontName;
            plt.Axes.Left.Label.FontName = fontName;
            plt.Axes.Right.Label.FontName = fontName;
            plt.Axes.Right.Label.OffsetX = 0;

            // 设置坐标轴刻度标签的字体
            plt.Axes.Bottom.TickLabelStyle.FontName = fontName;
            plt.Axes.Left.TickLabelStyle.FontName = fontName;
            plt.Axes.Right.TickLabelStyle.FontName = fontName;
            plt.Axes.Right.Label.OffsetX = 5;

            // 设置不同颜色区分
            plt.Axes.Left.Label.ForeColor = Color.FromARGB(0xFF3B88BD);
            plt.Axes.Right.Label.ForeColor = Color.FromARGB(0xFFFF7F0E);

            plt.Legend.FontName = fontName;
            plt.Legend.FontSize = 16;
            // 显示图例并设置位置
            plt.Legend.IsVisible = true;
            plt.Legend.Orientation = Orientation.Horizontal;
            plt.Legend.Alignment = Alignment.UpperCenter;
            // 半透明灰色背景（可以显示图线名称）
            plt.Legend.BackgroundFillStyle.Color = Color.FromARGB(50);
            // 使用透明的ARGB颜色（不遮挡折线图）
            plt.Legend.OutlineColor = Color.FromARGB(0);
            plt.Legend.ShadowColor = Color.FromARGB(0);
            // 设置标题(预留位置，放置Legend）
            plt.Title(".", size: 2);
            plt.Legend.Margin = new ScottPlot.PixelPadding(0, 0, 0, -30);

            // 保存图表为图片
            plt.SavePng(outputPath, width, height);
        }

        /// <summary>
        /// 生成双Y轴折线图(X轴为double值)
        /// </summary>
        /// <param name="xValues"></param>
        /// <param name="y1Values"></param>
        /// <param name="y2Values"></param>
        /// <param name="xAxisTitle"></param>
        /// <param name="y1AxisTitle"></param>
        /// <param name="y2AxisTitle"></param>
        /// <param name="outputPath"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static void GenerateDualLineChart(
            List<double> xValues,
            List<double> y1Values,
            List<double> y2Values,
            string xAxisTitle,
            string y1AxisTitle,
            string y2AxisTitle,
            string outputPath,
            int width = 800,
            int height = 500
        )
        {
            if (xValues.Count != y1Values.Count || xValues.Count != y2Values.Count)
            {
                throw new ArgumentException("X轴和Y轴的数据点数量必须相同");
            }

            // 创建一个新的绘图对象
            var myPlot = new Plot();

            // 使用 SignalXY 方法关联 X 轴和 Y 轴数据
            var sig1 = myPlot.Add.SignalXY(xValues.ToArray(), y1Values.ToArray());
            var sig2 = myPlot.Add.SignalXY(xValues.ToArray(), y2Values.ToArray());

            // 设置线条样式
            sig1.LineWidth = 2;
            sig2.LineWidth = 2;
            sig1.Color = Color.FromARGB(0xFF3B88BD);
            sig2.Color = Color.FromARGB(0xFFFF7F0E);

            // 设置轴关联
            sig1.Axes.YAxis = myPlot.Axes.Left;
            sig2.Axes.YAxis = myPlot.Axes.Right;

            // 设置图表标题和轴标题
            myPlot.XLabel(xAxisTitle, size: 18);
            myPlot.Axes.Left.Label.Text = y1AxisTitle;
            myPlot.Axes.Right.Label.Text = y2AxisTitle;

            // 设置字体和颜色
            string fontName = "宋体";
            myPlot.Axes.Title.Label.FontName = fontName;
            myPlot.Axes.Bottom.Label.FontName = fontName;
            myPlot.Axes.Left.Label.FontName = fontName;
            myPlot.Axes.Right.Label.FontName = fontName;

            // 设置坐标轴刻度标签的字体
            myPlot.Axes.Bottom.TickLabelStyle.FontSize = 16;
            myPlot.Axes.Left.TickLabelStyle.FontSize = 16;
            myPlot.Axes.Right.TickLabelStyle.FontSize = 16;
            myPlot.Axes.Bottom.TickLabelStyle.FontName = fontName;
            myPlot.Axes.Left.TickLabelStyle.FontName = fontName;
            myPlot.Axes.Right.TickLabelStyle.FontName = fontName;

            // 设置不同颜色区分
            myPlot.Axes.Left.Label.ForeColor = Color.FromARGB(0xFF3B88BD);
            myPlot.Axes.Right.Label.ForeColor = Color.FromARGB(0xFFFF7F0E);

            // 设置 X 轴的最小值和最大值
            myPlot.Axes.SetLimitsX(xValues.Min(), xValues.Max());

            // 尝试让右侧Y轴与图形右边重合
            myPlot.Axes.Right.Label.OffsetX = 0;

            // 保存图表为图片
            myPlot.SavePng(outputPath, width, height);
        }

        /// <summary>
        /// 生成分组柱状图 [注意横坐标超过10之后就2个单位显示一个数字]
        /// </summary>
        /// <param name="powerPoints">The powerPoints<see cref="List{string}"/></param>
        /// <param name="phaseData">The phaseData<see cref="List{List{double}}"/></param>
        /// <param name="phaseLabels">The phaseLabels<see cref="List{string}"/></param>
        /// <param name="xAxisTitle">X轴标题</param>
        /// <param name="yAxisTitle">Y轴标题</param>
        /// <param name="outputPath">输出路径</param>
        /// <param name="width">图表宽度</param>
        /// <param name="height">图表高度</param>
        public static void GenerateGroupedBarChart(
            List<string> powerPoints,
            List<List<double>> phaseData,
            List<string> phaseLabels,
            string xAxisTitle,
            string yAxisTitle,
            string outputPath,
            int width = 800,
            int height = 500
        )
        {
            if (phaseData.Count != phaseLabels.Count)
            {
                throw new ArgumentException("相数据集和标签数量必须相同");
            }

            foreach (var yValues in phaseData)
            {
                if (powerPoints.Count != yValues.Count)
                {
                    throw new ArgumentException("点数必须相同");
                }
            }

            // 创建一个新的绘图对象
            var plt = new Plot();

            // 设置柱状图的宽度和间距
            double groupWidth = 0.8; // 组的总宽度
            double barWidth = groupWidth / phaseData.Count * 0.6; // 单个柱子的宽度（减小为原来的0.6倍）
            double barSpacing = barWidth * 0.5; // 同组内柱子间距（增加为原来的0.5倍）
            double groupSpacing = 1.0; // 组间距

            // 为每个相设置不同的颜色
            var phaseColors = new Color[]
            {
                Color.FromHtml("#4169E1"), // A相颜色 - 蓝色
                Color.FromHtml("#FF8C00"), // B相颜色 - 橙色
                Color.FromHtml("#A9A9A9"), // C相颜色 - 灰色
            };
            double maxYValue = 0.0;
            double minYValue = double.MaxValue;

            // 为每个相（A、B、C）创建一组柱状图
            for (int phaseIndex = 0; phaseIndex < phaseData.Count; phaseIndex++)
            {
                if (phaseData[phaseIndex].Count != powerPoints.Count)
                {
                    throw new ArgumentException(
                        $"系列 {phaseIndex + 1} 的数据点数量与功率点数量不匹配"
                    );
                }

                double oneMax = phaseData[phaseIndex].Max();
                double oneMin = phaseData[phaseIndex].Min();
                if (oneMax > maxYValue)
                {
                    maxYValue = oneMax;
                }
                if (minYValue > oneMin)
                {
                    minYValue = oneMin;
                }

                // 计算每个柱子的位置
                double[] positions = new double[powerPoints.Count];
                double offset = 0;
                for (int j = 0; j < powerPoints.Count; j++)
                {
                    // 计算组的中心位置
                    double groupCenter = j * groupSpacing;

                    // 计算组内第一个柱子的左边缘位置
                    double firstBarLeft =
                        groupCenter
                        - (barWidth * phaseData.Count + barSpacing * (phaseData.Count - 1)) / 2;
                    if (j == 0 && firstBarLeft < 0)
                    {
                        offset = firstBarLeft * -1;
                        firstBarLeft = 0;
                    }
                    else if (j != 0 && offset != 0)
                    {
                        firstBarLeft += offset;
                    }

                    // 计算当前柱子的中心位置
                    positions[j] =
                        firstBarLeft + phaseIndex * (barWidth + barSpacing) + barWidth / 2;
                }

                List<Bar> bars = new List<Bar>();

                for (int k = 0; k < positions.Length; k++)
                {
                    Bar barOne = new Bar();
                    barOne.Position = positions[k];
                    barOne.Size = 0.2;
                    barOne.Value = phaseData[phaseIndex][k];
                    // 使用相对应的颜色，而不是功率点对应的颜色
                    barOne.FillColor = phaseColors[phaseIndex];
                    bars.Add(barOne);
                }

                // 添加柱状图
                var bar = plt.Add.Bars(bars);
                bar.LegendText = phaseLabels[phaseIndex];
            }

            // 设置X轴刻度标签为功率点
            // 当数据点超过10个时，每隔2个显示一个刻度标签
            if (powerPoints.Count > 10)
            {
                // 计算显示间隔
                int interval = 2; // 每隔2个点显示一个标签

                // 创建新的位置和标签数组
                var positions = new List<double>();
                var labels = new List<string>();

                for (int i = 0; i < powerPoints.Count; i += interval)
                {
                    positions.Add(i);
                    labels.Add(powerPoints[i]);
                }
                // 设置刻度生成器
                plt.Axes.Bottom.TickGenerator = new ScottPlot.TickGenerators.NumericManual(
                    positions.ToArray(),
                    labels.ToArray()
                );
            }
            else
            {
                // 数据点较少时，显示所有刻度标签
                plt.Axes.Bottom.TickGenerator = new ScottPlot.TickGenerators.NumericManual(
                    Enumerable.Range(0, powerPoints.Count).Select(i => (double)i).ToArray(),
                    powerPoints.ToArray()
                );
            }
            // Y
            plt.Axes.SetLimitsY(0, maxYValue * 1.2);

            //plt.Axes.RectifyY();

            // 设置图表标题和轴标签
            plt.XLabel(xAxisTitle, size: 18);
            plt.YLabel(yAxisTitle, size: 18);

            // 设置字体
            string fontName = "宋体";
            plt.Axes.Title.Label.FontName = fontName;
            plt.Axes.Bottom.Label.FontName = fontName;
            plt.Axes.Left.Label.FontName = fontName;
            plt.Axes.Bottom.TickLabelStyle.FontName = fontName;
            plt.Axes.Left.TickLabelStyle.FontName = fontName;

            // 显示图例
            plt.Legend.IsVisible = true;
            plt.Legend.Orientation = ScottPlot.Orientation.Horizontal;
            plt.Legend.Alignment = Alignment.UpperRight;
            plt.Legend.FontName = fontName;

            // 目录不存在，则自动创建
            string directoryPath = Path.GetDirectoryName(outputPath);
            if (!DirectoryUtil.IsExistDirectory(directoryPath))
            {
                DirectoryUtil.CreateDirectory(directoryPath);
            }

            // 保存图表为图片
            plt.SavePng(outputPath, width, height);
        }

        /// <summary>
        /// 生成单条折线图(X轴为DateTime值)
        /// </summary>
        /// <param name="xValues"></param>
        /// <param name="yValues"></param>
        /// <param name="chartTitle"></param>
        /// <param name="xAxisTitle"></param>
        /// <param name="yAxisTitle"></param>
        /// <param name="outputPath"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static void GenerateLineChart(
            List<DateTime> xValues,
            List<double> yValues,
            string xAxisTitle,
            string yAxisTitle,
            string outputPath,
            int width = 800,
            int height = 500
        )
        {
            if (xValues.Count != yValues.Count)
            {
                throw new ArgumentException("X轴和Y轴的数据点数量必须相同");
            }

            // 创建一个新的绘图对象
            var plt = new Plot();

            // 添加折线图数据并去除圆点
            var scatter = plt.Add.Scatter(xValues.ToArray(), yValues.ToArray());
            scatter.MarkerShape = MarkerShape.None;
            // 设置线条宽度
            scatter.LineWidth = 2;

            // 配置 X 轴为时间轴
            var axis = plt.Axes.DateTimeTicksBottom();
            var tickGen = (ScottPlot.TickGenerators.DateTimeAutomatic)axis.TickGenerator;
            static string CustomFormatter(DateTime dt)
            {
                return dt.ToString("HH:mm:ss");
            }
            tickGen.LabelFormatter = CustomFormatter;

            // 设置图表标题和轴标题
            plt.XLabel(xAxisTitle, size: 18);
            plt.YLabel(yAxisTitle, size: 18);
            // 设置X轴、Y轴刻度字体大小
            plt.Axes.Bottom.TickLabelStyle.FontSize = 16;
            plt.Axes.Left.TickLabelStyle.FontSize = 16;

            // 设置字体，确保使用支持中文的字体
            string fontName = "宋体";
            plt.Axes.Title.Label.FontName = fontName;
            plt.Axes.Bottom.Label.FontName = fontName;
            plt.Axes.Left.Label.FontName = fontName;
            // 设置坐标轴刻度标签的字体
            plt.Axes.Bottom.TickLabelStyle.FontName = fontName;
            plt.Axes.Left.TickLabelStyle.FontName = fontName;

            // 保存图表为图片
            plt.SavePng(outputPath, width, height);
        }

        /// <summary>
        /// 生成单条折线图(X轴为double值)
        /// </summary>
        /// <param name="xValues"></param>
        /// <param name="yValues"></param>
        /// <param name="xAxisTitle"></param>
        /// <param name="yAxisTitle"></param>
        /// <param name="outputPath"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static void GenerateLineChart(
            List<double> xValues,
            List<double> yValues,
            string xAxisTitle,
            string yAxisTitle,
            string outputPath,
            int width = 800,
            int height = 500
        )
        {
            if (xValues.Count != yValues.Count)
            {
                throw new ArgumentException("X轴和Y轴的数据点数量必须相同");
            }

            // 创建一个新的绘图对象
            var plt = new Plot();

            // 添加折线图数据
            var scatter = plt.Add.Scatter(xValues.ToArray(), yValues.ToArray());
            // 去除折线图上的原点
            scatter.MarkerShape = MarkerShape.None;
            // 设置线条宽度
            scatter.LineWidth = 2;

            // 设置图表标题和轴标题
            plt.XLabel(xAxisTitle, size: 18);
            plt.YLabel(yAxisTitle, size: 18);
            // 设置X轴、Y轴刻度字体大小
            plt.Axes.Bottom.TickLabelStyle.FontSize = 16;
            plt.Axes.Left.TickLabelStyle.FontSize = 16;
            // 设置 X 轴的最小值和最大值
            plt.Axes.SetLimitsX(xValues.Min(), xValues.Max());

            // 设置字体，确保使用支持中文的字体
            string fontName = "宋体";
            plt.Axes.Title.Label.FontName = fontName;
            plt.Axes.Bottom.Label.FontName = fontName;
            plt.Axes.Left.Label.FontName = fontName;
            // 设置坐标轴刻度标签的字体
            plt.Axes.Bottom.TickLabelStyle.FontName = fontName;
            plt.Axes.Left.TickLabelStyle.FontName = fontName;

            // 保存图表为图片
            plt.SavePng(outputPath, width, height);
        }

        /// <summary>
        /// The GenerateMultiLineChart
        /// </summary>
        /// <param name="xValues"></param>
        /// <param name="yValuesList"></param>
        /// <param name="lineLabels"></param>
        /// <param name="xAxisTitle"></param>
        /// <param name="yAxisTitle"></param>
        /// <param name="outputPath"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static void GenerateMultiLineChart(
            List<DateTime> xValues,
            List<List<double>> yValuesList,
            List<string> lineLabels,
            string xAxisTitle,
            string yAxisTitle,
            string outputPath,
            int width = 800,
            int height = 500
        )
        {
            if (yValuesList.Count != lineLabels.Count)
            {
                throw new ArgumentException("Y轴数据集和标签的数量必须相同");
            }

            foreach (var yValues in yValuesList)
            {
                if (xValues.Count != yValues.Count)
                {
                    throw new ArgumentException("X轴和Y轴的数据点数量必须相同");
                }
            }

            // 创建一个新的绘图对象
            var plt = new Plot();

            // 添加多条折线图数据
            for (int i = 0; i < yValuesList.Count; i++)
            {
                var scatter = plt.Add.Scatter(xValues.ToArray(), yValuesList[i].ToArray());
                scatter.MarkerShape = MarkerShape.None;
                // 设置线条宽度
                scatter.LineWidth = 2;
                scatter.LegendText = lineLabels[i];
            }

            // 配置 X 轴为时间轴
            var axis = plt.Axes.DateTimeTicksBottom();
            var tickGen = (ScottPlot.TickGenerators.DateTimeAutomatic)axis.TickGenerator;
            static string CustomFormatter(DateTime dt)
            {
                return dt.ToString("HH:mm:ss");
            }
            tickGen.LabelFormatter = CustomFormatter;

            // 设置图表标题和轴标题
            plt.XLabel(xAxisTitle, size: 18);
            plt.YLabel(yAxisTitle, size: 18);
            // 设置X轴、Y轴刻度字体大小
            plt.Axes.Bottom.TickLabelStyle.FontSize = 16;
            plt.Axes.Left.TickLabelStyle.FontSize = 16;
            // 设置图例字体大小
            plt.Legend.FontSize = 16;

            // 显示图例并设置位置
            plt.Legend.IsVisible = true;
            plt.Legend.Orientation = Orientation.Horizontal;
            plt.Legend.Alignment = Alignment.LowerCenter;
            // 半透明灰色背景（可以显示图线名称）
            plt.Legend.BackgroundFillStyle.Color = Color.FromARGB(50);
            // 使用透明的ARGB颜色（不遮挡折线图）
            plt.Legend.OutlineColor = Color.FromARGB(0);
            plt.Legend.ShadowColor = Color.FromARGB(0);

            // 设置字体，确保使用支持中文的字体
            string fontName = "宋体";
            plt.Axes.Title.Label.FontName = fontName;
            plt.Axes.Bottom.Label.FontName = fontName;
            plt.Axes.Left.Label.FontName = fontName;
            // 设置坐标轴刻度标签的字体
            plt.Axes.Bottom.TickLabelStyle.FontName = fontName;
            plt.Axes.Left.TickLabelStyle.FontName = fontName;
            // 设置图例字体
            plt.Legend.FontName = fontName;

            // 保存图表为图片
            plt.SavePng(outputPath, width, height);
        }

        /// <summary>
        /// 生成多条折线图(X轴为double值)
        /// </summary>
        /// <param name="xValues"></param>
        /// <param name="yValuesList"></param>
        /// <param name="lineLabels"></param>
        /// <param name="xAxisTitle"></param>
        /// <param name="yAxisTitle"></param>
        /// <param name="outputPath"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static void GenerateMultiLineChart(
            List<double> xValues,
            List<List<double>> yValuesList,
            List<string> lineLabels,
            string xAxisTitle,
            string yAxisTitle,
            string outputPath,
            int width = 800,
            int height = 500
        )
        {
            if (yValuesList.Count != lineLabels.Count)
            {
                throw new ArgumentException("Y轴数据集和标签的数量必须相同");
            }

            foreach (var yValues in yValuesList)
            {
                if (xValues.Count != yValues.Count)
                {
                    throw new ArgumentException("X轴和Y轴的数据点数量必须相同");
                }
            }
            // 若目录不存在，就自动创建
            string directoryPath = Path.GetDirectoryName(outputPath);
            if (!DirectoryUtil.IsExistDirectory(directoryPath))
            {
                DirectoryUtil.CreateDirectory(directoryPath);
            }

            // 创建一个新的绘图对象
            var plt = new Plot();

            // 添加多条折线图数据
            for (int i = 0; i < yValuesList.Count; i++)
            {
                var scatter = plt.Add.Scatter(xValues.ToArray(), yValuesList[i].ToArray());
                scatter.MarkerShape = MarkerShape.None;
                // 设置线条宽度
                scatter.LineWidth = 2;
                scatter.LegendText = lineLabels[i];
            }

            // 设置图表轴标题            
            plt.XLabel(xAxisTitle, size: 18);
            plt.YLabel(yAxisTitle, size: 18);
            // 设置X轴、Y轴刻度字体大小
            plt.Axes.Bottom.TickLabelStyle.FontSize = 16;
            plt.Axes.Left.TickLabelStyle.FontSize = 16;
            // 设置 X 轴的最小值和最大值
            plt.Axes.SetLimitsX(xValues.Min(), xValues.Max());
            // 设置图例字体大小
            plt.Legend.FontSize = 16;

            // 显示图例并设置位置
            plt.Legend.IsVisible = true;
            plt.Legend.Orientation = Orientation.Horizontal;
            plt.Legend.Alignment = Alignment.UpperCenter;
            // 半透明灰色背景（可以显示图线名称）
            plt.Legend.BackgroundFillStyle.Color = Color.FromARGB(50);
            // 使用透明的ARGB颜色（不遮挡折线图）
            plt.Legend.OutlineColor = Color.FromARGB(0);
            plt.Legend.ShadowColor = Color.FromARGB(0);
            // 设置标题(预留位置，放置Legend）
            plt.Title(".", size: 2);
            plt.Legend.Margin = new ScottPlot.PixelPadding(0, 0, 0, -30);

            // 设置字体，确保使用支持中文的字体
            string fontName = "宋体";
            plt.Axes.Title.Label.FontName = fontName;
            plt.Axes.Bottom.Label.FontName = fontName;
            plt.Axes.Left.Label.FontName = fontName;
            // 设置坐标轴刻度标签的字体
            plt.Axes.Bottom.TickLabelStyle.FontName = fontName;
            plt.Axes.Left.TickLabelStyle.FontName = fontName;
            // 设置图例字体
            plt.Legend.FontName = fontName;

            // 保存图表为图片
            plt.SavePng(outputPath, width, height);
        }

        /// <summary>
        /// 生成多条折线图(X轴为double值)
        /// </summary>
        /// <param name="xValues"></param>
        /// <param name="yValuesList"></param>
        /// <param name="lineLabels"></param>
        /// <param name="chartTitle"></param>
        /// <param name="xAxisTitle"></param>
        /// <param name="yAxisTitle"></param>
        /// <param name="outputPath"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public static void OxyPlotGenerateMultiLineChart(
            List<double> xValues,
            List<List<double>> yValuesList,
            List<string> lineLabels,
            string chartTitle,
            string xAxisTitle,
            string yAxisTitle,
            string outputPath,
            int width = 800,
            int height = 500
        )
        {
            if (yValuesList.Count != lineLabels.Count)
            {
                throw new ArgumentException("Y轴数据集和标签的数量必须相同");
            }

            foreach (var yValues in yValuesList)
            {
                if (xValues.Count != yValues.Count)
                {
                    throw new ArgumentException("X轴和Y轴的数据点数量必须相同");
                }
            }
            // 若目录不存在，就自动创建
            string directoryPath = Path.GetDirectoryName(outputPath);
            if (!DirectoryUtil.IsExistDirectory(directoryPath))
            {
                DirectoryUtil.CreateDirectory(directoryPath);
            }
            PlotModel plotModel = new PlotModel
            {
                Title = chartTitle,
                // 设置默认字体为宋体
                DefaultFont = "宋体",
                // 设置标题字体为宋体
                TitleFont = "宋体"
            };

            plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = xAxisTitle, Font="宋体"});
            plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = yAxisTitle, Font = "宋体" });

            List<(string, List<OxyPlot.DataPoint>)> seriesData = CreateDataPoints(xValues, yValuesList, lineLabels);

            // 添加折线系列
            foreach (var item in seriesData)
            {
                var series = new LineSeries
                {
                    Title = item.Item1,
                    MarkerType = MarkerType.None,
                    LineStyle = OxyPlot.LineStyle.Solid,
                    ItemsSource = item.Item2
                };

                plotModel.Series.Add(series);
            }
            plotModel.Legends.Add(new OxyPlot.Legends.Legend
            {
                LegendPosition = LegendPosition.BottomCenter,
                LegendOrientation = LegendOrientation.Horizontal,
                LegendPlacement = LegendPlacement.Outside,
                Font = "宋体"
            });

            var exporter = new PngExporter { Width = width, Height = height };
             
     
            exporter.ExportToFile(plotModel, outputPath);
        }

        /// <summary>
        /// 设置刻度值的格式
        /// </summary>
        /// <param name="range"></param>
        /// <returns></returns>
        private static string CalcTickFormat(double range)
        {
            if (range < 0.1)
            {
                return "F3";
            }
            else if (range < 1)
            {
                return "F2";
            }
            else if (range < 10)
            {
                return "F1";
            }
            else
            {
                return "F0";
            }
        }

        /// <summary>
        /// The CreateDataPoints
        /// </summary>
        /// <param name="xValues">The xValues<see cref="List{double}"/></param>
        /// <param name="yValuesList">The yValuesList<see cref="List{List{double}}"/></param>
        /// <param name="lineLabels">The lineLabels<see cref="List{string}"/></param>
        /// <returns>The <see cref="List{(string,List{OxyPlot.DataPoint})}"/></returns>
        private static List<(string, List<OxyPlot.DataPoint>)> CreateDataPoints(List<double> xValues,
            List<List<double>> yValuesList,
            List<string> lineLabels)
        {
            List<(string, List<OxyPlot.DataPoint>)> results = new List<(string, List<OxyPlot.DataPoint>)>();
            int count = lineLabels.Count();
            int j = 0;
            foreach (List<double> oneyList in yValuesList)
            {
                List<OxyPlot.DataPoint> dataPoints = new List<OxyPlot.DataPoint>();
                for (int i = 0; i < oneyList.Count; i++)
                {
                    dataPoints.Add(new OxyPlot.DataPoint(xValues[i], oneyList[i]));
                }

                results.Add((lineLabels[j], dataPoints));
                j++;
            }
            return results;
        }
    }
}
